package com.whut.mobilesafe.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.whut.mobilesafe.db.BlackListOpenHelper;
import com.whut.mobilesafe.domain.BlackListInFo;

import java.util.ArrayList;

/**
 * 作    者:yubo.xiaoyubo
 * 时    间：2015/12/25 9:18
 * 版    本：1.0
 * 描    述：
 */
public class BlackListDao {
    private static final String TABLENAME = "blackNumber";

    private static BlackListDao sDao = null;
    private BlackListOpenHelper mHelper;

    private BlackListDao(Context ctx) {
        mHelper = new BlackListOpenHelper(ctx);

    }

    ;

    public static BlackListDao getInstance(Context ctx) {
        if (sDao == null) {
            synchronized (BlackListDao.class) {
                if (sDao == null) {
                    sDao = new BlackListDao(ctx);
                }
            }
        }
        return sDao;
    }


    public void add(String number, int mode) {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("number", number);
        values.put("mode", mode);
        db.insert(TABLENAME, null, values);
        db.close();
    }

    public void delete(String number) {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        db.delete(TABLENAME, "number = ?", new String[]{number});
        db.close();
    }

    public void update(String number, int mode) {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("number", number);
        values.put("mode", mode);
        db.update(TABLENAME, values, "number = ?", new String[]{number});
        db.close();
    }

    public ArrayList<BlackListInFo> queryAll() {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        Cursor cursor = db.query(TABLENAME, null, null, null, null, null, null);
        ArrayList<BlackListInFo> inFos = new ArrayList<BlackListInFo>();
        while (cursor.moveToNext()) {
            String number = cursor.getString(cursor.getColumnIndex("number"));
            int mode = cursor.getInt(cursor.getColumnIndex("mode"));
            BlackListInFo inFo = new BlackListInFo(number, mode);
            inFos.add(inFo);
        }
        db.close();
        return inFos;
    }

    public int getCount() {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        Cursor cursor = db.query(TABLENAME, null, null, null, null, null, null);
        return cursor.getCount();
    }

    //分页查询。从index 开始 查询20条记录
    public ArrayList<BlackListInFo> pageQuery(int index) {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLENAME + " order by _id desc limit ?,20", new String[]{index + ""});
        ArrayList<BlackListInFo> infos = new ArrayList<>();
        while (cursor.moveToNext()) {
            String number = cursor.getString(cursor.getColumnIndex("number"));
            int mode = cursor.getInt(cursor.getColumnIndex("mode"));
            BlackListInFo inFo = new BlackListInFo(number, mode);
            infos.add(inFo);
        }
        return infos;
    }

    //根据当前电话号码判断拦截的类型
    public int getModeByNumber(String number) {
        int mode = -1;
        SQLiteDatabase db = mHelper.getWritableDatabase();

        Cursor cursor = db.query(TABLENAME, new String[]{"mode"}, "number = ?", new String[]{number}, null, null, null);
        if (cursor.moveToNext()) {
            mode = cursor.getInt(cursor.getColumnIndex("mode"));
        }
        return mode;
    }


}
